package com.huadi.ssm.controller;

/**
 * 系统主页控制器
 *
 */

import com.huadi.ssm.po.Admin;
import com.huadi.ssm.po.Expert;
import com.huadi.ssm.po.Researcher;
import com.huadi.ssm.service.AdminService;
import com.huadi.ssm.service.ExpertService;
import com.huadi.ssm.service.ResearcherService;
import com.huadi.ssm.util.CpachaUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@RequestMapping("/system")
@Controller
public class SystemController {

    @Autowired
    private AdminService adminService;
    @Autowired
    private ResearcherService researcherService;
    @Autowired
    private ExpertService expertService;


    @RequestMapping(value = "/index",method=RequestMethod.GET)
    public ModelAndView index(ModelAndView model){
        model.setViewName("system/index");
        return model;
    }

    /**
     * 登陆页面
     * @param model
     * @return
     */
    @RequestMapping(value = "/login",method= RequestMethod.GET)
    public ModelAndView login(ModelAndView model){
        model.setViewName("system/login");
        return model;
    }

    /**
     * 注销登录
     * @param request
     * @return
     */
    @RequestMapping(value = "/login_out",method=RequestMethod.GET)
    public String loginOut(HttpServletRequest request){
        request.getSession().setAttribute("user", null);
        return "redirect:login";
    }

    /**
     * 登录表单提交
     * @return
     */
    @RequestMapping(value = "/login",method=RequestMethod.POST)
    @ResponseBody
    public Map<String, String> login(
            @RequestParam(value="name",required=true) String name,
            @RequestParam(value="password",required=true) String password,
            @RequestParam(value="vcode",required=true) String vcode,
            @RequestParam(value="type",required=true) int type,
            HttpServletRequest request
    ){
        Map<String, String> ret = new HashMap<String, String>();
        if(StringUtils.isEmpty(name)){
            ret.put("type", "error");
            ret.put("msg", "用户名不能为空!");
            return ret;
        }
        if(StringUtils.isEmpty(password)){
            ret.put("type", "error");
            ret.put("msg", "密码不能为空!");
            return ret;
        }
        if(StringUtils.isEmpty(vcode)){
            ret.put("type", "error");
            ret.put("msg", "验证码不能为空!");
            return ret;
        }
        String loginCpacha = (String)request.getSession().getAttribute("loginCpacha");
        if(StringUtils.isEmpty(loginCpacha)){
            ret.put("type", "error");
            ret.put("msg", "长时间未操作，会话已失效，请刷新后重试!");
            return ret;
        }
        if(!vcode.toUpperCase().equals(loginCpacha.toUpperCase())){
            ret.put("type", "error");
            ret.put("msg", "验证码错误!");
            return ret;
        }
        request.getSession().setAttribute("loginCpacha", null);
        //从数据库中去查找用户
        if(type == 1){
            //管理员
            Admin admin = adminService.findByName(name);
            if(admin == null){
                ret.put("type", "error");
                ret.put("msg", "不存在该管理员!");
                return ret;
            }
            if(!password.equals(admin.getPassword())){
                ret.put("type", "error");
                ret.put("msg", "密码错误!");
                return ret;
            }
            request.getSession().setAttribute("user", admin);
        }
        if(type == 2){
            //科研工作人员
            Researcher researcher = researcherService.findByName(name);
            if(researcher == null){
                ret.put("type", "error");
                ret.put("msg", "不存在该科研工作人员!");
                return ret;
            }
            if(!password.equals(researcher.getPassword())){
                ret.put("type", "error");
                ret.put("msg", "密码错误!");
                return ret;
            }
            request.getSession().setAttribute("user", researcher);
        }
        if(type == 3){
            //评审专家
            Expert expert = expertService.findByName(name);
            if(expert == null){
                ret.put("type", "error");
                ret.put("msg", "不存在该评审专家!");
                return ret;
            }
            if(!password.equals(expert.getPassword())){
                ret.put("type", "error");
                ret.put("msg", "密码错误!");
                return ret;
            }
            request.getSession().setAttribute("user", expert);
        }
        request.getSession().setAttribute("userType", type);
        ret.put("type", "success");
        ret.put("msg", "登录成功!");
        return ret;
    }

    /**
     * 显示 验证码
     * @param request
     * @param vl
     * @param w
     * @param h
     * @param response
     */
    @RequestMapping(value="/get_cpacha",method=RequestMethod.GET)
    public void getCpacha(HttpServletRequest request,
                          @RequestParam(value="vl",defaultValue="4",required=false) Integer vl,
                          @RequestParam(value="w",defaultValue="98",required=false) Integer w,
                          @RequestParam(value="h",defaultValue="33",required=false) Integer h,
                          HttpServletResponse response){
        CpachaUtil cpachaUtil = new CpachaUtil(vl, w, h);
        String generatorVCode = cpachaUtil.generatorVCode();
        request.getSession().setAttribute("loginCpacha", generatorVCode);
        BufferedImage generatorRotateVCodeImage = cpachaUtil.generatorRotateVCodeImage(generatorVCode, true);
        try {
            ImageIO.write(generatorRotateVCodeImage, "gif", response.getOutputStream());
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}
